package cc.rengu.igas.opms.common.dao.impl;

import cc.rengu.igas.opms.common.dao.BankSupportCfgMapper;
import cc.rengu.igas.opms.common.entity.BankSupportCfgInfo;
import cc.rengu.oltp.service.common.constant.AppConfigConstant;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.utility.util.DbsUtil;
import cc.rengu.oltp.utility.util.XmlConfigUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.cache.UnifiedCache;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 支持银行列表配置表(T_P_BANK_SUPPORT_CFG) 操作实现
 * <p>
 * Created by keyi on 2020/3/27.
 */
public class BankSupportCfgMapperImpl implements BankSupportCfgMapper {

    String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppConfigConstant.DATABASE_CONF_NODE, AppConfigConstant.OLTP_POOL_NAME);

    @Override
    public List<BankSupportCfgInfo> selectOpmsBankSupportCfgByBankNoSupportBizType(String instId, String bankNo, String supportBizType) throws Exception {

        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.OLTP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "opmsGetBankSupportCfgByBankNoSupportBizType");

        BankSupportCfgInfo info = new BankSupportCfgInfo();
        info.setInstId(instId);
        info.setBankNo(bankNo);
        info.setSupportBizType(supportBizType);

        List<Object> objects = dbsUtil.dbsFetchLimitListData(sqlStr, info, BankSupportCfgInfo.class);
        return objects.stream().map(item -> (BankSupportCfgInfo) item).collect(Collectors.toList());
    }

    @Override
    public List<BankSupportCfgInfo> selectOpmsBankSupportCfgByInstIdSupportBizType(String instId, String supportBizType) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.OLTP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "opmsGetBankSupportCfgByInstIdSupportBizType");

        BankSupportCfgInfo info = new BankSupportCfgInfo();
        info.setInstId(instId);
        info.setSupportBizType(supportBizType);

        List<Object> objects = dbsUtil.dbsFetchLimitListData(sqlStr, info, BankSupportCfgInfo.class);
        return objects.stream().map(item -> (BankSupportCfgInfo) item).collect(Collectors.toList());
    }

    @Override
    public List<BankSupportCfgInfo> selectOpmsBankSupportCfgByInstIdBankNo(String instId, String bankNo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.OLTP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectOpmsBankSupportCfgByInstIdBankNo");

        BankSupportCfgInfo info = new BankSupportCfgInfo();
        info.setInstId(instId);
        info.setBankNo(bankNo);

        List<Object> objects = dbsUtil.dbsFetchLimitListData(sqlStr, info, BankSupportCfgInfo.class);
        return objects.stream().map(item -> (BankSupportCfgInfo) item).collect(Collectors.toList());
    }
}
